(function($){
	var Widget = {
		
		//CheckMenu
		Menu : {
			
			options : {},			
			
			run : function(){
				var self = this, links = $(self.options['links']),
					list = self.options['datasource'],
					_id = self.options['id'],
					relate = self.options['relate'],
					output = $(self.options['output']);
					
				//请选择产品的实现
				links.bind('click',function(){
					var me = $(this);
					var filterItem = me.parent().find(relate).val();
					var filterData = [], menu = {}, tempArr;					
					
					if($.trim(filterItem) != ''){
						tempArr = filterItem.split('|');
						for(var i=0,j=tempArr.length;i<j;i++){
							filterData.push({'id':tempArr[i]});
						}
					}
					
					for(var m = 0;m<list.length;m++){
						menu[list[m]['id']] = list[m];
					} 
					
					$(".dialogCloseBtn a").click();
					
					$.CheckMenu.open({
						'id' : _id,
						'triggerjQueryBtn' : me,	//触点按钮,支持多个相同处理的对象
						'dataList' : menu,		//展示数据
						'filterDataList' : filterData,	//筛选数据
						'outputItemjQueryDiv' : output,		//单项选择输出位置DOM对象
						'showDiv' : $("#tempPanel"),
						'template' : '<li><a $selected$ href="javascript:void(0);" id="$id$">$name$<em></em></a></li>'		//初始化HTML
				
					},function(m){		
						m.itemToAddCallBack = function(item){
							var id = item.id.replace('show_'+ _id +'_','');
							var val = $(relate).val();
							var rs = [];
							
							if(val == ''){
								$(relate).val(id);					
							}else{
								val = $(relate).val().split('|');
								
								for(var i=0;i<val.length;i++){
									if(val[i] != id){
										rs.push(val[i]);
									}	
								}
								
								rs.push(id);
								$(relate).val(rs.join('|'));
							}			
						};
						m.itemToDeleteCallBack = function(item){
							var id = item.id.replace('show_'+ _id +'_','');
							var val = $(relate).val().split('|');
							var rs = [];
							
							for(var i=0;i<val.length;i++){
								if(val[i] != id){
									rs.push(val[i]);
								}	
							}
							
							if(rs.length > 1){
								$(relate).val(rs.join('|'));					
							}else{
								$(relate).val(rs);
							}			
						};
						m.outputItemDeleteCallBack = function(item){
							
							var id = item.id.replace('output_'+ _id +'_','');
							var val = $(relate).val().split('|');
							var rs = [];
							
							for(var i=0;i<val.length;i++){
								if(val[i] != id){
									rs.push(val[i]);
								}	
							}
							
							if(rs.length > 1){
								$(relate).val(rs.join('|'));					
							}else{
								$(relate).val(rs);
							}
							
							$("#show_0_" + id).removeClass('selected');		
						};
						m.closeCallBack = function(){
							delete m;			
						};
					});
				});
				
				if($.trim($(relate).val()) != ''){
					$(links).click();					
					$("#checkmenu_"+ _id +" .selected").click().click();
					$(".dialogCloseBtn a").click();
				}

			},
			
			//调用分发器
			init : function(opt){				
				this.options = $.extend(this.options,opt);				
				this.run();				
			}						
		}
	};
	
	$.MyWidget = Widget;
})(jQuery);